package com.android.lucky.adapter

import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import com.android.lucky.R
import com.android.lucky.entity.LuckyEntity
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * FileName: LuckyAdapter
 * Author: admin
 * Date: 2021/2/6 15:01
 * Description:
 */
class LuckyAdapter(rvWidth: Int) :
    BaseQuickAdapter<LuckyEntity, BaseViewHolder>(R.layout.item_lucky, null) {
    private var width = 0
    private var selectPos = -1

    init {
        width = rvWidth
    }

    override fun getDefItemViewType(position: Int): Int {
        return position
    }

    override fun convert(holder: BaseViewHolder, item: LuckyEntity) {
        val context = holder.itemView.context
        val position = holder.layoutPosition
        val itemImg = holder.getView<AppCompatImageView>(R.id.itemImg)
        val itemTv = holder.getView<AppCompatTextView>(R.id.itemTv)
        val layoutItem = holder.getView<RelativeLayout>(R.id.layoutItem)

        Glide.with(holder.itemView.context)
            .load(item.res)
            .into(itemImg)
        itemTv.text = item.value

        val lp = layoutItem.layoutParams as LinearLayout.LayoutParams
        lp.weight = (width / 3).toFloat()
        lp.height = (lp.weight / 5 * 3).toInt()
        layoutItem.requestLayout()

        if (selectPos == position) {
            holder.itemView.setBackgroundResource(R.drawable.lib_lucky_bg_item_select)
        } else {
            holder.itemView.setBackgroundResource(R.drawable.lib_lucky_bg_item)
        }

    }

    fun setItemSelectBg(pos: Int) {
        selectPos = getPos(pos)
        notifyDataSetChanged()
    }

    fun getPos(pos:Int):Int{
        var p = 0
        when (pos) {
            0 -> {
                p = 0
            }
            1 -> {
                p = 1
            }
            2 -> {
                p = 2
            }
            3 -> {
                p = 5
            }
            4 -> {
                p = 8
            }
            5 -> {
                p = 7
            }
            6 -> {
                p = 6
            }
            7 -> {
                p = 3
            }
        }
        return p
    }
}